package com.weibo.login.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.weibo.common.BaseSeverlet;
import com.weibo.common.DESCoder;
import com.weibo.common.Params;
import com.weibo.login.service.LoginService;
import com.weibo.login.service.imp.LoginServiceImp;
import com.weibo.sql.CommonParm;
import com.weibo.user.User;

public class LoginServlet extends BaseSeverlet {
	private static Logger logger = Logger.getLogger(LoginServlet.class);
	private LoginService loginService = new LoginServiceImp();
	@Override
	public void doService(HttpServletRequest request,HttpServletResponse response) {
		// TODO Auto-generated method stub
		Object method = request.getParameter("method");
		
		if("login".equals(method)){
			login(request,response);
		}
		
	}
	
	/**
	 * 用户登陆验证
	 * @param request
	 * @param response
	 */
	public void login(HttpServletRequest request,HttpServletResponse response){
		String password = request.getParameter("password");
		String username = request.getParameter("username");
		String code = request.getParameter("code");
		String is_submit_token = request.getParameter("is_submit_token")==null?"":(String)request.getParameter("is_submit_token");
		String code_validate = request.getSession().getAttribute("rand")==null?"":(String)request.getSession().getAttribute("rand");
		if(password==null){
			password="";
		}
		if(username==null){
			username="";
		}
		if(code==null){
			code="";
		}
		if(!(password.equals("")||username.equals("")||code.equals(""))){
			if(code.equals(code_validate)&&password!=null&&username!=null){
				try {
					password = DESCoder.encryptBASE64(DESCoder.encrypt(DESCoder.decryptBASE64(password), Params.VALIDATE_KEY));
				} catch (Exception e1) {
					// TODO Auto-generated catch block
					logger.error(e1);
				}
				User user = loginService.validate(username, password.trim());
				if(user!=null){
					//合法用户
					request.getSession().setAttribute("login_user", user);
					request.getSession().setAttribute("is_submit_token", is_submit_token);
					try {
						//request.setAttribute("user_login", user);
						loginService.updateLoginDate(user.getUser_id());
						response.sendRedirect(request.getContextPath()+"/MyweiboServlet.do");
					} catch (IOException e) {
						// TODO Auto-generated catch block
						logger.error(e);
					}
				}else{
					//非法用户
					try {
						request.setAttribute("login_info", CommonParm.LOGIN_USERNAMEORPASSWRD_ERROR);
						request.getRequestDispatcher("/index.jsp").forward(request, response);
					} catch (ServletException e) {
						// TODO Auto-generated catch block
						logger.error(e);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						logger.error(e);
					}
				}
			}else{
				//验证码错误
				try {
					request.setAttribute("login_info", CommonParm.LOGIN_CODE_ERROR);
					request.getRequestDispatcher("/index.jsp").forward(request, response);
				} catch (ServletException e) {
					// TODO Auto-generated catch block
					logger.error(e);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					logger.error(e);
				}
			}
		}else{
			request.setAttribute("login_info", CommonParm.LOGIN_TIMEOUT);
			try {
				request.getRequestDispatcher("/index.jsp").forward(request, response);
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				logger.error(e);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				logger.error(e);
			}
		}
	}

}
